package {
	import com.xjw.ArrayHelp;
	
	import flash.display.Sprite;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import org.adobe.utils.StrBytes;
	import org.adobe.utils.ArrayUtil;

	public class ChineseByte extends Sprite {
		private var byte:ByteArray = new ByteArray;
		
		public function ChineseByte(){
			var byte:ByteArray = StrBytes.toByteArray("537563636573733a20537472696e672072652d656e636f6465642e");
			var str:String = byte.toString();
			trace(str);
			
			
			/*var myTest:String = "AAA";
			var byte:ByteArray = StrToByteArray(myTest);
			var strTest:String = byArrayToString(byte);
			trace(strTest);*/
		}
		/**字符转为byte数据并返回byte，写入字符的charCode值*/
		public static function StrToByteArray(strValue:String, uLen:uint = 0):ByteArray {
			var byte:ByteArray = new ByteArray();
			byte.endian = Endian.LITTLE_ENDIAN;
			
			for (var i:int = 0; i < strValue.length; i++){
				trace("code:" + strValue.charCodeAt(i),strValue.charCodeAt(i).toString(16))
				byte.writeShort(strValue.charCodeAt(i));  		//将字符转为code将16位整数写入byte
			}
			return byte;
		}

		/**byte转为 unicode字符,使用fromCharCode()转换字符*/
		public static function byArrayToString(byArray:ByteArray):String {
			var strReturn:String = "";
			byArray.position = 0;
			while(byArray.bytesAvailable > 0){
				var char:int = byArray.readShort();
				strReturn += String.fromCharCode(char);  //将code转换为字符串
			}
			
			/*var strReturn:String = new String();
			for (var i:int = 0; i < byArray.length / 2; ++i){
				var strRep:String = byArray.readMultiByte(2, "unicode");
				strReturn = replaceAt(strReturn, strRep, i, i);
			}*/
			return strReturn;
		}

		public static function replaceAt(char:String, value:String, beginIndex:int, endIndex:int):String {
			beginIndex = Math.max(beginIndex, 0);
			endIndex = Math.min(endIndex, char.length);
			var firstPart:String = char.substr(0, beginIndex);
			var secondPart:String = char.substr(endIndex, char.length);
			return (firstPart + value + secondPart);
		}
		
		
		/**
		 * 随机生成汉字
		 * 转字节数组组合转换为字符串 ba.writeShort(parseInt(str,16));
		 * byte.writeMultiByte(str,charset);
		 * byte数组---读取转为16进制---组合中文区位编码---写入16位无符整数---读取类型
		 */
		/*
		//写入
		var byte:ByteArray =new ByteArray();
		byte.writeMultiByte("钟","gbk2312");
		for(var j:int=0; j<byte.length; j++){
			trace(byte[j],byte[j].toString(16));  //214 211 D6D3 5451  54区51个
		}
		
		//
		var ba:ByteArray = new ByteArray;
		ba.writeShort(parseInt("0xA1D3",16));
		ba.position = 0;
		trace(ba.readMultiByte(ba.length,"gbk"));
		*/
		public static function StrToByte(byte:ByteArray):String {
			var result:String = "";
			var ba:ByteArray = new ByteArray();
			byte.position = 0;
			for(var i:int=0; i<byte.length; i+=2){
				var str:String = "0x" + byte[i].toString(16) + byte[i+1].toString(16)
				ba.writeShort(parseInt(str,16));    //组合一个16进制整数
				
				ba.position = 0;
				result += ba.readMultiByte(ba.length,"gb2312")
				ba.clear();
			}
			return result;
		}
		/**
		 *  trace(unescape("%u60E0%u666E"))
			trace(escape("惠普"))    //%u60E0%u666E
			trace(encodeURI("惠普")) //%E6%83%A0%E6%99%AE
			trace(encodeURIComponent("惠普"));
		 * 
		 */
		public static function urlEncode(str:String,code:String):String {
			var result:String = "";
			
			var byte:ByteArray =new ByteArray();
			byte.writeMultiByte(str,code);
			for (var i:int; i<byte.length; i++) {
				//trace(byte[i],String.fromCharCode(byte[i]))  
				
				//对字节escape
				result +=  escape(String.fromCharCode(byte[i]));
			}
			return result;
		}
	}
}